package com.chzc.fm.duotin.ui.other;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.ChannelType;
import com.chzc.fm.duotin.ui.channel.ChannelFragment;
import com.chzc.fm.duotin.ui.home.HomeFragment;
import com.chzc.fm.duotin.ui.mine.MineFragment;
import com.chzc.fm.duotin.ui.more.MoreFragment;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private RelativeLayout layoutContent;
    private HomeFragment homeFragment;
    private ChannelFragment channelFragment;
    private MineFragment mineFragment;
    private MoreFragment moreFragment;
    private BaseFragment[] fragments;
    private List<LinearLayout> layoutTabs;
    private int[] ids = {
            R.id.ll_tab_home,
            R.id.btn_channel,
            R.id.btn_mine,
            R.id.btn_more
    };

    private int index;
    private int currentTabIndex;

    //与startLoginActivity相关的变量
    private Runnable run;
    private Handler handler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    protected void initViews() {
        layoutTabs = new ArrayList<>();
        for (int i = 0; i<ids.length; i++){
            LinearLayout layout = (LinearLayout) this.findViewById(ids[i]);
            layoutTabs.add(layout);
        }
    }

    protected void initEvents() {

    }

    protected void init() {

        homeFragment = new HomeFragment();
        channelFragment = new ChannelFragment();
        mineFragment = new MineFragment();
        moreFragment = new MoreFragment();

        fragments = new BaseFragment[]{
                homeFragment,
                channelFragment,
                mineFragment,
                moreFragment};

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_content_rv, homeFragment)
                .add(R.id.fragment_content_rv, channelFragment)
                .add(R.id.fragment_content_rv, mineFragment)
                .add(R.id.fragment_content_rv, moreFragment)
                .show(homeFragment)
                .hide(channelFragment)
                .hide(mineFragment)
                .hide(moreFragment)
                .commit();

        layoutTabs.get(0).setSelected(true);

        showLoginActivity();
    }

    private void showLoginActivity() {
        if(GlobalParam.USER_KEY == ""){
            run = new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(i);
                    overridePendingTransition(R.anim.activity_open, 0);
                }
            };
            handler.postDelayed(run, 3000);
        }
    }

    public void onTabSelect(View view) {
        switch (view.getId()) {
            case R.id.ll_tab_home:
                index = 0;
                onTabIndex(index);
                break;
            case R.id.btn_channel:
                index = 1;
                onTabIndex(index);
                break;
            case R.id.btn_mine:
                index = 2;
                onTabIndex(index);
                break;
            case R.id.btn_more:
                index = 3;
                onTabIndex(index);
                break;
        }
    }

    private void onTabIndex(int index){
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_content_rv, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }

        layoutTabs.get(currentTabIndex).setSelected(false);
        layoutTabs.get(index).setSelected(true);
        currentTabIndex = index;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_close, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == 3)
        {
            List<ChannelType> type = (List<ChannelType>) data.getExtras().getSerializable("typelist");
            LogUtil.w("type.size() >>>" + type.size());
            channelFragment.upDateUi(type);
        }
    }
}
